package com.xintoucloud.pbs.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xintoucloud.pbs.entity.DynamicNews;
import com.xintoucloud.pbs.entity.DynamicNewsTopic;
import com.xintoucloud.pbs.entity.MessageSend;
import com.xintoucloud.pbs.model.DynamicNewsShowVo;
import com.xintoucloud.pbs.model.MessageSendVo;
import com.xintoucloud.pbs.service.MessageSendService;
import com.xintoucloud.pbs.service.NewsManagementService;
import com.xintoucloud.pbs.service.NewsTopicManagementService;

@Controller
@RequestMapping("/MessageSendController")
public class MessageSendController {
	
	@Autowired
	private NewsManagementService nmService;
	@Autowired
	private NewsTopicManagementService ntmService;
	@Autowired
	private MessageSendService messageSendService;
	
	
	@RequestMapping("/index")   //连接先进到控制器, 返回值在进入要显示数据的html页面NewsManagement2
	public String index(Model ovo) {
		ovo.addAttribute("ovo", messageSendService.orgVoList());
		return "messageSend";
	}

	@RequestMapping("/other")   //连接先进到控制器, 返回值在进入要显示数据的html页面NewsManagement2
	public String other() {
		return "NewsView";
	}
	
	//展示所有通知消息
	@RequestMapping("/list")  //NewsTopicManagement2页面读取其中的数据,当读到URL:list时, 进入到该方法,
	@ResponseBody			//把数据封装到map中,返回的map会被自动解析	
	public Map<String ,Object> list() {
		Map<String,Object> map = new HashMap<String,Object>();
		List<MessageSendVo> list = messageSendService.viewList();
		map.put("code", 0);   //1,code:解析接口状态
		map.put("data",list); //2,msg:解析提示文本
		return map;			  //3,count:解析数据长度
							  //4,data:解析数据列表
	}

	
	//添加消息
	@RequestMapping("/add")
	public String TopAdd(HttpServletRequest request) {
		MessageSend ms = new MessageSend();
		String title = request.getParameter("title");
		String context = request.getParameter("context");
		
		StringBuffer buf = new StringBuffer();
		buf.append("");
		String[] oids = request.getParameterValues("oids");
		for(String oid: oids) {
			buf.append(","+oid);
		}
		String buff = buf.subSequence(1, buf.length()).toString();
		
		Date createtime = new Date();
		ms.setCreatetime(createtime);
		ms.setTitle(title);
		ms.setOids(buff);
		ms.setContext(context);
		messageSendService.add(ms);
		return "redirect:index";  //重定向到界面
	}

}
